// 这里放一些通用的js方法
// 引入基础请求地址
import {
	baseurl
} from "@/config/baseUrl.js"

// 获取地块信息-主要获取地块亩数-和通过单位获取批次的批次单位-最后进行组合
function getfacarea(facid) {
	if (!facid) {
		return "暂无数据"
	}
	// 解决api请求头改变是，第一次调用还是旧的带数据方式
	var zhnewheader = {
		'sysDataBaseName': "DB_TDW1_SC",
		'content-type': 'application/json',
		'accessToken': uni.getStorageSync("token") || '',
		'databaseName': uni.getStorageSync("databaseName") || '',
		'organizationNo': uni.getStorageSync("organizationNo") || 'JiuGuan',
		'organizationList': uni.getStorageSync("organizationNo") || 'JiuGuan',
		'virtualSystemNo': uni.getStorageSync("virtualSystemNo") || 'app'
	}
	// 组织结构idlist设置
	let neworglist = uni.getStorageSync("organizationList")
	if (neworglist) {
		let neworgstr = ""
		neworglist.forEach((neworgitem, neworgindex) => {
			neworgstr = neworgstr + neworgitem
			if (neworgindex < neworglist.length - 1) {
				neworgstr += ","
			} else {
				zhnewheader.organizationList = neworgstr
			}
		})
	}
	return new Promise((resolve, reject) => {
		// 获取地块详情
		// 当兄弟看到这一段的时候，证明你准备知道怎么用了
		uni.request({
			url:  (uni.getStorageSync('baseurl') || baseurl) + "v3/facility/facility/queryByNo?no=" + facid +
				`&organizationNo=${uni.getStorageSync("organizationNo")|| 'JiuGuan'}`,
			method: "GET",
			data: {},
			header: zhnewheader,
			success: (res) => {
				let munumber = 0
				let nowunitNo = 1
				let nowunitname = ""
				if (res.data.data) {
					if (res.data.data.scope) {
						munumber = res.data.data.scope
					}
					if (res.data.data.unitNo) {
						nowunitNo = res.data.data.unitNo
					}
					uni.request({
						url: (uni.getStorageSync('baseurl') || baseurl) + "v3/templateTenant/unit/queryByNo?no=" + nowunitNo +
							`&organizationNo=${uni.getStorageSync("organizationNo")|| 'JiuGuan'}`,
						method: "GET",
						success: (restwo) => {
							nowunitname = restwo.data.data.unitName
							let areamessage = munumber + nowunitname
							resolve(areamessage)
						},
						fail(err) {
							nowunitname = "亩"
							let areamessage = munumber + nowunitname
							resolve(areamessage)
						}
					})
				}
			},
			fail: (res) => {
				uni.showToast({
					title: "网络请求错误啦",
					icon: 'none'
				})
				reject(res)
			}
		})
	})
}

// 获取地块信息
function getfacareas(facid) {
	if (!facid) {
		return "暂无数据"
	}
	// 解决api请求头改变是，第一次调用还是旧的带数据方式
	var zhnewheader = {
		'sysDataBaseName': "DB_TDW1_SC",
		'content-type': 'application/json',
		'accessToken': uni.getStorageSync("token") || '',
		'databaseName': uni.getStorageSync("databaseName") || '',
		'organizationNo': uni.getStorageSync("organizationNo") || 'JiuGuan',
		'organizationList': uni.getStorageSync("organizationNo") || 'JiuGuan',
		'virtualSystemNo': uni.getStorageSync("virtualSystemNo") || 'app'
	}
	// 组织结构idlist设置
	let neworglist = uni.getStorageSync("organizationList")
	if (neworglist) {
		let neworgstr = ""
		neworglist.forEach((neworgitem, neworgindex) => {
			neworgstr = neworgstr + neworgitem
			if (neworgindex < neworglist.length - 1) {
				neworgstr += ","
			} else {
				zhnewheader.organizationList = neworgstr
			}
		})
	}
	return new Promise((resolve, reject) => {
		// 获取地块详情
		// 当兄弟看到这一段的时候，证明你准备知道怎么用了
		uni.request({
			url: (uni.getStorageSync('baseurl') || baseurl) + "v3/facility/facility/queryByNo?no=" + facid +
				`&organizationNo=${uni.getStorageSync("organizationNo")|| 'JiuGuan'}`,
			method: "GET",
			data: {},
			header: zhnewheader,
			success: (res) => {
				let munumber = 0
				let nowunitNo = 1
				let nowunitname = ""
				if (res.data.data) {
					if (res.data.data.scope) {
						munumber = res.data.data.scope
					}
					if (res.data.data.unitNo) {
						nowunitNo = res.data.data.unitNo
					}
					resolve(res.data.data)
				}
			},
			fail: (res) => {
				uni.showToast({
					title: "网络请求错误啦",
					icon: 'none'
				})
				reject(res)
			}
		})
	})
}
//  获取控制页面按钮
function getButton(no, btnNo) {
	if (!no&&!btnNo) {
		return false
	}
	// 解决api请求头改变是，第一次调用还是旧的带数据方式
	var zhnewheader = {
		'content-type': 'application/json',
		'token': uni.getStorageSync("token") || '',
	}
	// 组织结构idlist设置
	let neworglist = uni.getStorageSync("organizationList")
	if (neworglist) {
		let neworgstr = ""
		neworglist.forEach((neworgitem, neworgindex) => {
			neworgstr = neworgstr + neworgitem
			if (neworgindex < neworglist.length - 1) {
				neworgstr += ","
			} else {
				zhnewheader.organizationList = neworgstr
			}
		})
	}
	return new Promise((resolve, reject) => {
		// 获取地块详情
		// 当兄弟看到这一段的时候，证明你准备知道怎么用了
		uni.request({
			url: (uni.getStorageSync('baseurl') || baseurl) + "v3/authority/v1/pageElement/queryPageElement",
			method: "GET",
			data: {},
			header: zhnewheader,
			success: (res) => {
				if (res.data.data) {
					let list = res.data.data
					let status = false
					list.forEach(item => {
						if (JSON.stringify(no) === JSON.stringify(item.pageElementNo) && JSON.stringify(btnNo) === JSON.stringify(item.pageNo)) {
							status = true
						}
					})
					resolve(status)
				}
			},
			fail: (res) => {
				uni.showToast({
					title: "网络请求错误啦",
					icon: 'none'
				})
				reject(res)
			}
		})
	})
}

const GetNowTime = (time, type) => {
	if (time) {
		var date = new Date(time)
	} else {
		var date = new Date()
	}

	var year = date.getFullYear()
	var month = date.getMonth() + 1
	var day = date.getDate()
	var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours()
	var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()
	var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
	month >= 1 && month <= 9 ? (month = "0" + month) : "";
	day >= 0 && day <= 9 ? (day = "0" + day) : "";
	if (type == 1) {
		var timer = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
	} else {
		var timer = year + '-' + month + '-' + day;
	}
	return timer;
}
module.exports = {
	getfacarea: getfacarea,
	getfacareas: getfacareas,
	GetNowTime: GetNowTime,
	getButton: getButton,
}
